/**
 * Created by xinhuang on 15/6/12.
 */
(function (angular) {
  'use strict';

  angular.module('services.customActivity.realtime')
    .service('chartService', chartService);

  function chartService(BUSINESS_LINE_NAME) {
    var that = this;
    var _options = {
      tooltip: {trigger: 'axis'},
      title: {text: '', x: 'center', textStyle: {fontSize: 14}, padding: 0},
      legend: {show: false, data: []},
      grid: {borderWidth: 0, y: '12%', x: '8%', x2: '8%'},
      toolbox: {show: false},
      xAxis: [{type: 'category', data: [], axisLabel: {interval: 0}}],
      yAxis: [{
        type: 'value',
        name: '交易金额(元)',
        splitLine: {show: false},
        axisLabel: {show: true, interval: 1, textStyle: {baseline: "top"}}
      }, {type: 'value', name: '业务量(笔)', axisLabel: {show: true, interval: 1, textStyle: {baseline: "top"}}}],
      series: [{name: '业务量(笔)', yAxisIndex: 1, type: 'bar', data: []}, {name: '交易金额(元)', type: 'line', data: []}]
    };

    that.busTimeVol = {
      init: busTimeVolInit,
      update: busTimeVolUpdate,
      instance: {}
    };
    that.userRechargePrefer = {
      init: userRechargeInit,
      update: userRechargeUpdate,
      instance: {}
    };
    that.province = {
      init: provinceInit,
      update: provinceUpdate,
      instance: {}
    };
    that.health = {
      init: healthInit,
      update: healthUpdate,
      instance: {}
    };

    that.clearInstances = clearInstances;

    function clearInstances() {
      that.busTimeVol.instance = {};
      that.userRechargePrefer.instance = {};
      that.province.instance = {};
      that.health.instance = {};
    }

    function busTimeVolInit(dom, busLine, initData, state) {
      if (dom === null) {
        return;
      }
      var options = angular.copy(_options);
      options.title.text = BUSINESS_LINE_NAME[busLine.toUpperCase()];

      if (state === '1005') {
        options.xAxis[0].data = initData.xAxis;
        options.xAxis[0].axisLabel.interval = 2;
        options.series[0].data = initData.data[0];
        options.series[1].data = initData.data[1];
      } else {

        for (var i = 0; i < 23; i++) {
          options.xAxis[0].data.push('');
          options.series[0].data.push(0);
          options.series[1].data.push(0);
        }

        options.xAxis[0].data.push(initData.xAxis);
        options.series[0].data.push(initData.data[0]);
        options.series[1].data.push(initData.data[1]);
      }

      if (busLine === 'upay' || busLine === 'tmall') {
        options.yAxis[0].name = '交易金额(万元)';
        options.series[1].name = '交易金额(万元)';
      }

      that.busTimeVol.instance[busLine] = echarts.init(dom);
      that.busTimeVol.instance[busLine].setOption(options);
    }

    function busTimeVolUpdate(busLine, resData, state) {
      var chart = that.busTimeVol.instance[busLine];
      var options = angular.copy(_options);
      if (state === '1005') {
        options.xAxis.axisLabel.interval = 2;
        options.series[0].data = resData.data[0];
        options.series[1].data = resData.data[1];
        chart.setOption(options);
      } else {
        chart.setOption({xAxis: [{axisLabel: {interval: 2}}]}, false);
        chart.addData([
          [0, resData.data[0], false, false],
          [1, resData.data[1], false, false, resData.xAxis]
        ]);
      }
    }

    function userRechargeInit(dom, busLine, initData) {
      if (dom === null) {
        return;
      }
      var options = {
        tooltip: {trigger: 'yxis'},
        title: {text: '', x: 'center', textStyle: {fontSize: 14}, padding: 0},
        toolbox: {show: false},
        grid: {borderWidth: 0, y: '12%', x: '12%', x2: '12%'},
        xAxis: [{type: 'value', name: '交易量(笔)'}],
        yAxis: [{
          type: 'category',
          name: '充值金额(元)',
          data: [],
          axisLabel: {show: true, interval: 0, textStyle: {baseline: "center"}}
        }],
        series: [{name: '交易量', type: 'bar', data: []}]
      };

      options.xAxis[0].name = initData.flag === '1' ? '交易量(万笔)' : '交易量(笔)';

      options.title.text = BUSINESS_LINE_NAME[busLine.toUpperCase()];

      options.yAxis[0].data = initData.yAxis;
      options.series[0].data = initData.data;

      that.userRechargePrefer.instance[busLine] = echarts.init(dom);
      that.userRechargePrefer.instance[busLine].setOption(options);
    }

    function userRechargeUpdate(busLine, resData) {
      var options = {
        series: [{name: '交易量', type: 'bar', data: resData}]
      };
      that.userRechargePrefer.instance[busLine].setOption(options, false);
    }

    function provinceInit(dom, busLine, initData) {
      if (dom === null) {
        return;
      }
      //判断是成功率还是业务量
      var _state = angular.element(dom).attr('id').indexOf('Successrate') !== -1 ? 'rateState' : 'volState';
      var options = {
        title: {
          show: false
        },
        tooltip: {
          trigger: 'item',
          formatter: function (params) {
            if (params[2] === '-') {
              return '暂不统计';
            }
            if (busLine === 'provinceSuccessrate') {
              params[2] = params[2].toFixed(2);
              return params[1] + '<br/>' + params[2] + '%';
            }
            return params[1] + '<br/>' + params[2] + '';
          }
        },
        legend: {
          show: false,
          data: []
        },
        toolbox: {
          show: false
        },
        series: [
          {
            name: '',
            type: 'map',
            mapType: 'china',
            itemStyle: {
              normal: {color: '#eee', borderColor: '#fff', borderWidth: '.7', label: {show: true}}
            },
            clickable: true,
            mapLocation: {y: 'top', height: 280},
            data: []
          },
          {
            name: '',
            type: 'map',
            mapType: 'china',
            clickable: true,
            itemStyle: {
              normal: {color: '#f44336', borderColor: '#fff', borderWidth: '.7', label: {show: true}}
            },
            mapLocation: {y: 'top', height: 280},
            data: []
          },
          {
            name: '',
            type: 'map',
            mapType: 'china',
            clickable: true,
            itemStyle: {
              normal: {color: '#2196f3', borderColor: '#fff', borderWidth: '.7', label: {show: true}}
            },
            mapLocation: {y: 'top', height: 280},
            data: []
          }
        ]
      };
      angular.forEach(initData, function (item) {
        var _item = {};
        _item.name = item.provinceName;
        _item.value = busLine === 'provinceSuccessrate' ? item.rate.toString() : item.volume.toString();
        options.series[item[_state]].data.push(_item);
      });

      that.province.instance[busLine] = echarts.init(dom);
      that.province.instance[busLine].setOption(options);
    }

    function provinceUpdate(dom, busLine, resData) {
      //判断是成功率还是业务量
      var _state = angular.element(dom).attr('id').indexOf('Successrate') !== -1 ? 'rateState' : 'volState';
      var options = {
        tooltip: {trigger: 'item', show: true},
        series: [
          {data: []},
          {data: []},
          {data: []}
        ]
      };

      angular.forEach(resData, function (item) {
        var _item = {};
        _item.name = item.provinceName;
        _item.value = busLine === 'provinceSuccessrate' ? item.rate.toString() : item.volume.toString();
        options.series[item[_state]].data.push(_item);
      });

      that.province.instance[busLine].setOption(options, false);
    }

    function healthInit(dom, initData) {
      if (dom === null) {
        return;
      }
      var colors = ['#4caf50', '#2196f3', '#ffeb3b', '#f44336'];
      var option = {
        tooltip: {
          show: false
        },
        legend: {
          data: [],
          show: false
        },
        toolbox: {
          show: false
        },
        series: [
          {
            type: 'pie', radius: ['50%', '68%'], center: ['50%', '50%'], clickable: false,
            data: [
              {
                value: 20,
                name: '',
                itemStyle: {
                  normal: {
                    label: {textStyle: {color: '#607d8b', fontSize: 12}},
                    borderWidth: '2',
                    borderColor: "#fff",
                    labelLine: {lineStyle: {color: '#607d8b'}},
                    color: '#4caf50'
                  },
                  emphasis: {borderWidth: '2', borderColor: "#fff", color: 'transparent'}
                }
              },
              {
                value: 20,
                name: '',
                itemStyle: {
                  normal: {
                    label: {textStyle: {color: '#607d8b', fontSize: 12}},
                    borderWidth: '2',
                    borderColor: "#fff",
                    labelLine: {lineStyle: {color: '#607d8b'}},
                    color: '#4caf50'
                  },
                  emphasis: {borderWidth: '2', borderColor: "#fff", color: 'transparent'}
                }
              },
              {
                value: 20,
                name: '',
                itemStyle: {
                  normal: {
                    label: {textStyle: {color: '#607d8b', fontSize: 12}},
                    borderWidth: '2',
                    borderColor: "#fff",
                    labelLine: {lineStyle: {color: '#607d8b'}},
                    color: '#4caf50'
                  },
                  emphasis: {borderWidth: '2', borderColor: "#fff", color: 'transparent'}
                }
              },
              {
                value: 20,
                name: '',
                itemStyle: {
                  normal: {
                    label: {textStyle: {color: '#607d8b', fontSize: 12}},
                    borderWidth: '2',
                    borderColor: "#fff",
                    labelLine: {lineStyle: {color: '#607d8b'}},
                    color: '#4caf50'
                  },
                  emphasis: {borderWidth: '2', borderColor: "#fff", color: 'transparent'}
                }
              },
              {
                value: 20,
                name: '',
                itemStyle: {
                  normal: {
                    label: {textStyle: {color: '#607d8b', fontSize: 12}},
                    borderWidth: '2',
                    borderColor: "#fff",
                    labelLine: {lineStyle: {color: '#607d8b'}},
                    color: '#4caf50'
                  },
                  emphasis: {borderWidth: '2', borderColor: "#fff", color: 'transparent'}
                }
              }
            ]
          }]
      };

      option.series[0].data[0].name = '移动商城\n' + initData.scpay + '%';
      option.series[0].data[1].name = '银行总对总\n' + initData.bank + '%';
      option.series[0].data[2].name = '天猫旗舰店\n' + initData.tmall + '%';
      option.series[0].data[3].name = '有价卡\n' + initData.vces + '%';
      option.series[0].data[4].name = '灵犀语音\n' + initData.lingxi + '%';

      option.series[0].data[0].itemStyle.normal.color = colors[initData.scpayState - 1];
      option.series[0].data[1].itemStyle.normal.color = colors[initData.bankState - 1];
      option.series[0].data[2].itemStyle.normal.color = colors[initData.tmallState - 1];
      option.series[0].data[3].itemStyle.normal.color = colors[initData.vcesState - 1];
      option.series[0].data[4].itemStyle.normal.color = colors[initData.lingxiState - 1];

      that.health.instance = echarts.init(dom, 'macarons');
      that.health.instance.setOption(option);

    }

    function healthUpdate(data) {
      var option = {
        series: [
          {
            data: [
              {itemStyle: {normal: {}}},
              {itemStyle: {normal: {}}},
              {itemStyle: {normal: {}}},
              {itemStyle: {normal: {}}},
              {itemStyle: {normal: {}}}
            ]
          }]
      };

      var colors = ['#4caf50', '#2196f3', '#ffeb3b', '#f44336'];

      option.series[0].data[0].name = '移动商城\n' + data.scpay + '%';
      option.series[0].data[1].name = '银行总对总\n' + data.bank + '%';
      option.series[0].data[2].name = '天猫旗舰店\n' + data.tmall + '%';
      option.series[0].data[3].name = '有价卡\n' + data.vces + '%';
      option.series[0].data[4].name = '灵犀语音\n' + data.lingxi + '%';

      option.series[0].data[0].itemStyle.normal.color = colors[data.scpayState - 1];
      option.series[0].data[1].itemStyle.normal.color = colors[data.bankState - 1];
      option.series[0].data[2].itemStyle.normal.color = colors[data.tmallState - 1];
      option.series[0].data[3].itemStyle.normal.color = colors[data.vcesState - 1];
      option.series[0].data[4].itemStyle.normal.color = colors[data.lingxiState - 1];

      that.health.instance.setOption(option, false);
    }
  }

})(angular);
